package cn.com.bluemoon.daps.model.core.file.parser;

import cn.com.bluemoon.daps.model.core.file.IMetaData;
import cn.com.bluemoon.daps.model.core.file.config.GlobalParserConfig;
import cn.com.bluemoon.daps.model.core.file.report.ImportBaseInfo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jarod.Kong
 * @date 2021/3/11 13:57
 */
public abstract class AbstractFileParser implements FileParser {
    public static final String PARSER_VALiD_ERROR_KEY = "parser_error_key";

    private GlobalParserConfig globalParserConfig;

    public AbstractFileParser() {
    }

    public AbstractFileParser(GlobalParserConfig globalParserConfig) {

        this.globalParserConfig = globalParserConfig;

    }

    public void setGlobalParserConfig(GlobalParserConfig globalParserConfig) {
        this.globalParserConfig = globalParserConfig;
    }

    public GlobalParserConfig getGlobalParserConfig() {
        return globalParserConfig;
    }

    /**
     * 构建错误的excel基础信息
     *
     * @param sheetTotal 总数
     * @param errorData  错误信息
     * @param sheetName  页名
     * @param sheetNo    页号
     * @param <T>        x
     * @return x
     */
    @SuppressWarnings("rawtypes")
    public static <T extends IMetaData> ImportBaseInfo getImportBaseInfo(int sheetTotal, List<T> errorData, String sheetName, int sheetNo) {
        List<IMetaData> metaDatas = errorData.stream().map(IMetaData::simpleMetadata).collect(Collectors.toList());
        return ImportBaseInfo.builder().errors(metaDatas)
                .result(Boolean.FALSE).errorSize(metaDatas.size()).pageName(sheetName)
                .pageNo(sheetNo).total(sheetTotal).build();
    }
}
